
//  DMRegistAController.m
//  DMGME
//
//  Created by ihope99 on 15/9/7.
//  Copyright (c) 2015年 GYP. All rights reserved.
//

#import "DMRegistAController.h"
#import "DMRegistCController.h"
#import "DMRegistDController.h"
#import "DMHomeViewController.h"


@interface DMRegistAController ()

@end

@implementation DMRegistAController
{
    NSTimer *timer;
    int seconds;
    UILabel *lableTime;


}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"注册"];
    

    
    lableTime = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _getCodeNumBtn.frame.size.width, _getCodeNumBtn.frame.size.height)];
//    _getCodeNumBtn.enabled = NO;

    [lableTime setTextAlignment:NSTextAlignmentCenter];
    [_getCodeNumBtn addSubview:lableTime];
    //15687878798

    self.view.backgroundColor = [UIColor whiteColor];
    [self AddLeftImageBtn:[UIImage imageNamed:@"fanhui_03 2"]target:self action:@selector(GoBack)];
    _phoneNumTextF.inputAccessoryView = [self GetInputAccessoryView];
    _phoneNumTextF.keyboardType = UIKeyboardTypeNumberPad;
   
    _codeNumTextF.inputAccessoryView = [self GetInputAccessoryView];
    _codeNumTextF.keyboardType = UIKeyboardTypeNumberPad;

    _mNameTextF.inputAccessoryView = [self GetInputAccessoryView];
    
    [_getCodeNumBtn addTarget:self action:@selector(getCodeNumBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_okBtn addTarget:self action:@selector(okBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)timerFireMethod:(NSTimer *)theTimer {
    if (seconds == 0) {
        [theTimer invalidate];
        [lableTime setText:@""];
        [_getCodeNumBtn setBackgroundImage:[UIImage imageNamed:@"getPhoneCodeButton"] forState:UIControlStateSelected];
        [_getCodeNumBtn setEnabled:YES];
    }else{
        seconds--;
        [_getCodeNumBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_getCodeNumBtn setEnabled:NO];
        
        [_getCodeNumBtn setBackgroundImage:[UIImage imageNamed:@"15@2x_13"] forState:UIControlStateDisabled];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [lableTime setText:[NSString stringWithFormat:@"%d秒",seconds]];
        });
    }
}

- (void)ShowAlert:(NSString *)text {
    UILabel *msg = [[UILabel alloc] initWithFrame:CGRectMake((self.view.bounds.size.width-250)/2, (self.view.bounds.size.height-80)/2, 250, 80)];
    msg.text = text;
    msg.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.7];
    msg.textColor = [UIColor whiteColor];
    msg.font = [UIFont systemFontOfSize:14];
    msg.numberOfLines = 0;
    msg.textAlignment = NSTextAlignmentCenter;
    msg.layer.cornerRadius = 10;
    msg.clipsToBounds = YES;
    
    [self.view addSubview:msg];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [msg removeFromSuperview];
        [self.navigationController popViewControllerAnimated:YES];

    });
}

- (void)okBtnClick{
//    NSLog(@"%@",_mNameTextF.text);
    
    if (!_phoneNumTextF.text || _phoneNumTextF.text.length == 0) {
        [self showMsg:@"请输入您的手机号"];
    }else if (!_codeNumTextF.text|| _codeNumTextF.text.length == 0) {
        [self showMsg:@"请输入手机验证码"];
    }else if (!_mNameTextF.text|| _mNameTextF.text.length == 0){
        [self showMsg:@"请输入您的姓名"];
    }else{
    
    if ([_shopID isKindOfClass:[NSNull class]]) {
        DMRegistCController *dmregisVC = [[DMRegistCController alloc] initWithNibName:@"DMRegistCController" bundle:nil];
        dmregisVC.codeID = _codeNumTextF.text;
        dmregisVC.phoneNumstr = _phoneNumTextF.text;
        dmregisVC.mNamestr = _mNameTextF.text;
        [self.navigationController pushViewController:dmregisVC animated:YES];
    }else{

        if ([_status isEqualToString:@"0"]) {
            [self ShowAlert:@"您已经注册过该手机号了，请前往登录页面"];
        }else if ([_status isEqualToString:@"1"]&&[_statusPassword isEqualToString:@""]){
        DMRegistDController *dmHome = [[DMRegistDController alloc] init];
            dmHome.titleType = @"3";
            dmHome.shopID = _statusShopID;
        [self.navigationController pushViewController:dmHome animated:YES];
        }else if ([_status isEqualToString:@"1"]&&![_statusPassword isEqualToString:@""]){
            [self ShowAlert:@"您已经注册过该手机号了，请前往登录页面"];
 
//            NSLog(@"2222");
        }
      }
    }
    
}
- (void)getCodeNumBtnClick
{
    if (!_phoneNumTextF.text || _phoneNumTextF.text.length == 0) {
        [self showMsg:@"请输入手机号"];
    }else{
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
    seconds = 60;
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$captcha"] forKey:@"token"];
        [dict setValue: _phoneNumTextF.text forKey:@"mobile"];
        [dict setValue: @"captcha" forKey:@"action"];
    
        [AFDownManager POST:REGISTERNET parameters:dict success:^(id responseObject) {
//            NSLog(@"注册 == %@",responseObject);
           
            if ([responseObject[@"code"] intValue] == 0) {
//                _codeNumTextF.text = responseObject[@"data"][@"captcha"];

                _shopID = responseObject[@"data"][@"agent"];
                if (![_shopID isKindOfClass:[NSNull class]]) {
                    _status = responseObject[@"data"][@"agent"][@"status"];
                    _statusShopID = responseObject[@"data"][@"agent"][@"id"];
                    _statusPassword = responseObject[@"data"][@"agent"][@"password"];
                }
            }else{
                [self showMsg:responseObject[@"desc"]];
            }
            
//            NSLog(@"%@",responseObject[@"desc"]);
            
        } failure:^(NSError *error) {
        }];
    }
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
